package com.app.newiptv.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.adaper.CollectAdapter;
import com.app.newiptv.adaper.LatelyAdapter;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.LatelyPresenter;
import com.app.newiptv.utils.SystemTimeDate;


public class LatelyActivity extends Activity implements Handler.Callback, View.OnClickListener {
    private RecyclerView mLately_rv;
    private RecyclerView mCollect_rv;
    private LatelyPresenter latelyPresenter;
    private ImageView focuseView;
    private TextView timer_view;
    private Handler handler;
    private TextView search_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_lately);
        initView();
        initData();
        setLisenter();
    }

    private void initView() {
        mLately_rv = (RecyclerView) findViewById(R.id.lately_rv);
        mCollect_rv = (RecyclerView) findViewById(R.id.collect_rv);
        focuseView = (ImageView)this.findViewById(R.id.focus_view);
        timer_view = (TextView) findViewById(R.id.timer_view);
        search_btn = (TextView) findViewById(R.id.search_btn);
    }

    private void initData() {
        handler = new Handler(this);
        handler.sendEmptyMessage(0);
        timer_view.setTypeface(MyApplication.face);
        latelyPresenter = new LatelyPresenter(this);
        latelyPresenter.setDisplayed(true);
        latelyPresenter.loadItem(new Bundle(), latelyCallback);
    }

    private void setLisenter() {
        search_btn.setOnClickListener(this);
        search_btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    search_btn.setBackgroundResource(R.mipmap.recommen_search_s);
                }else{
                    search_btn.setBackgroundResource(R.mipmap.recoomen_search);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        latelyPresenter.getCacheRecord();
        latelyPresenter.getCollectCache();
    }

    private LatelyPresenter.Callback latelyCallback = new LatelyPresenter.Callback() {
        @Override
        public void onError(int code, String msg) {

        }

        @Override
        public void onMovieCompleted(LatelyPresenter presenter, int count) {
            LatelyAdapter latelyAdapter = new LatelyAdapter(presenter, LatelyActivity.this);
            latelyAdapter.setCount(count);
            latelyAdapter.setFocusView(focuseView);
            LinearLayoutManager manager = new LinearLayoutManager(LatelyActivity.this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mLately_rv.setLayoutManager(manager);
            mLately_rv.setAdapter(latelyAdapter);
            mLately_rv.post(new Runnable() {
                @Override
                public void run() {
                    if (mLately_rv.getChildCount() > 0) {
                        mLately_rv.getChildAt(0).requestFocus();
                    }
                }
            });
        }

        @Override
        public void onCollectionCompleted(LatelyPresenter presenter, int count) {
            CollectAdapter collectAdapter = new CollectAdapter(presenter, focuseView, LatelyActivity.this);
            collectAdapter.setCount(count);
            LinearLayoutManager manager = new LinearLayoutManager(LatelyActivity.this);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mCollect_rv.setLayoutManager(manager);
            mCollect_rv.setAdapter(collectAdapter);
            mCollect_rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        if (mCollect_rv.getChildCount() > 0) {
                            mCollect_rv.getChildAt(0).requestFocus();
                        }
                    }
                }
            });
        }
    };

    @Override
    public boolean handleMessage(Message message) {
        timer_view.setText(SystemTimeDate.stampToHHmmDate(String.valueOf(System.currentTimeMillis())));
        handler.sendEmptyMessageDelayed(0, 20 * 1000);
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_btn:
                startActivity(new Intent(this, SearchActivity.class));
                break;
        }
    }
}
